package com.ltw.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.ltw.modules.common.entity.Result;
import com.ltw.modules.common.entity.SearchBean;
import com.ltw.modules.test.entity.City;
import com.ltw.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")

public class CityController {
   @Autowired
    private CityService cityService;
    /*
     * 127.0.0.1/api/city ----post
     * {"cityName":"dream city","localCityName":"梦想之城" ,"countryId":522}
     * */

    @PostMapping(value = "/city",consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city){
        return  cityService.insertCity(city);
    }

    /*
     * 127.0.0.1/api/city ----put
     * cityName:dreamCity1,localCityName=梦想之城1,cityId=2258}
     * */
    @PutMapping(value = "/city" ,consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city){

        return cityService.updateCity(city);
    }


    /*
     * 127.0.0.1/api/city/2256 ----put
     * cityName:dreamCity1,localCityName=梦想之城1,countryId=2258}
     * */
    @DeleteMapping("/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId ){
        return cityService.deleteCityByCityId(cityId);
    }


    /*
     * 127.0.0.1/api/city/2249----get
     *
     * */
    @GetMapping("/city/{cityId}")
    public City queryCityByCityId(@PathVariable int cityId){
        return cityService.queryCityByCityId(cityId);
    }


    /*
     * 127.0.0.1/api/city?cityName=Shanghai&localCityName=1111----get
     *
     * */
    @GetMapping("/city")
    public City queryCityByCityNameAndLocalCityName(String cityName, String localCityName){
        return cityService.queryCityByCityNameAndLocalCityName(cityName,localCityName);
    }

    /*
     * 127.0.0.1/api/cities ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"city_name","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/cities", consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return cityService.getCitiesBySearchBean(searchBean);
    }
}
